<template>
	<view class="addEditForm">
		<view class="data-form bgfff">
			<view class="data-form-item flex-ac">
				<text class="data-form-label f30">标题</text>
				<view class="data-form-input">
					<input type="text" :disabled="optionDisabled" v-model="dataForm.title"
						:placeholder="optionDisabled?'-':'请输入标题'" placeholder-class="plStyle" />
				</view>
			</view>
			<view class="data-form-item flex-ac">
				<text class="data-form-label f30">记录文件</text>
				<view class="data-form-input">
					<gui-upload-images2 @change="choosePhoto" fileName="file" ref="uploadimgcom" @uploaded="uploaded"
						:uploadServerUrl="`${$http.config.baseUrl}/common/upload`"
						:maxFileNumber="1"></gui-upload-images2>
				</view>
			</view>
		</view>
		<view class="right-area-footer2 flex-ac bgfff" v-if="!optionDisabled">
			<view class="saveBtn cancleBtn" @tap="handleCancle">取消</view>
			<view class="saveBtn" @tap="handleSubmit">保存</view>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				needPploadedImgs: '',
				did: '',
				dataForm: { //表单
					id: '',
					title: '',
					handleFile: ''
				},
				cpddDateRangeVal: '',
				cpddDateRangeVal2: '',
				selectList: [], //上一页带过来，如果上一页用不到，可以在这里调用赋值
				optionDisabled: false,
			}
		},
		onLoad(options) {
			console.log('options', options);
			this.did = options.id || ''
			this.optionDisabled = options.op == 'look' ? true : false
			let title = options.op == 'look' ? '详情' : options.op == 'add' ? '新增' : '编辑'
			uni.setNavigationBarTitle({
				title
			})
			if (options.op != 'add') {
				this.loadDetail();
			}
		},
		methods: {
			loadDetail() {
				this.$http.get(`/process/kitchenWaste/${this.did}`).then(res => {
					if (res.code == 200) {
						Object.keys(this.dataForm).forEach(key => this.dataForm[key] = res.data[key])
						this.$refs.uploadimgcom.setItems([res.data.handleFile]);
					}
				})
			},
			handleCancle() {
				this.$u.back()
			},
			handleSubmit() {
				console.log('提交');
				if (!this.dataForm.title) return this.$u.msg("请输入标题")
				if (this.needPploadedImgs.length < 1) {
					this.$u.msg("请上传记录文件")
					return;
				}
				if (this.needPploadedImgs.length > 0) {
					this.$refs.uploadimgcom.upload();
					return;
				} else {
					this.realSumbit();
				}
			},
			realSumbit() {
				if (this.dataForm.id) {
					this.$http.put('/process/kitchenWaste', this.dataForm).then(res => {
						if (res.code == 200) {
							uni.$emit('reFlashList')
							this.$u.msg('修改成功')
							this.$u.back(1, 1000)
						}
					})
				} else {
					this.$http.post('/process/kitchenWaste', this.dataForm).then(res => {
						if (res.code == 200) {
							uni.$emit('reFlashList')
							this.$u.msg('已新增')
							this.$u.back(1, 1000)
						}
					})
				}
			},
			choosePhoto(e) { //选择图片
				console.log(e);
				this.needPploadedImgs = e;
			}, // 图片上传完毕执行 uploaded 函数
			uploaded: function(uploadedImgs) {
				console.log('图片上传完毕，开始提交数据');
				// 组合数据给后端 api 提交
				console.log(uploadedImgs);
				let arr = []
				for (var i = 0; i < uploadedImgs.length; i++) {
					arr.push(uploadedImgs[i].url)
				}
				this.dataForm.handleFile = arr.join();
				this.realSumbit();
			},
		}
	}
</script>

<style>

</style>